<?php
$dbHost = 'localhost'; 
$dbUser = 'root'; 
$dbPass = '123';
$dbDatabase = 'rdb'; 
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());


$error = array();
$results = array();


if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); 
   
   if (strlen($searchTerms) ==0) {
      $error[] = "Search terms must not be zero.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); 
   }
   
  
   if (count($error) < 1) {
      $searchSQL = "SELECT Branch_Code,Branch_Name, Address  FROM branch WHERE ";
      
      
      $types = array();
	  $types[] = isset($_GET['Branch_Code'])?"`Branch_Code` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['Branch_Name'])?"`Branch_Name` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['Address'])?"`Address` LIKE '%{$searchTermDB}%'":'';
      
      
      $types = array_filter($types, "removeEmpty"); 
      
      if (count($types) < 1)
         $types[] = "`Address` LIKE '%{$searchTermDB}%'"; 
      
          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `Branch_Code`"; 

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms}  no results.";
      }else {
         $results = array(); 
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: {$row['Branch_Code']}<br />{$row['Branch_Name']}<br />{$row['Address']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>
<html>
   <title>My Advance Search Form</title>
   <style type="text/css">
      #error {
         color: red;
      }
   </style>
   <body>
   <b> Branch Search</b><p></p>
      <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="GET" action="BranchSearch.php" name="searchForm">
        Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
        Search In:<br />
        Branch_Code: <input type="checkbox" name="Branch_Code" value="on" <?php echo isset($_GET['Branch_Code'])?"checked":''; ?> />
        Branch_Name: <input type="checkbox" name="Branch_Name" value="on" <?php echo isset($_GET['Branch_Name'])?"checked":''; ?> /> | 
        Address: <input type="checkbox" name="Address" value="on" <?php echo isset($_GET['Address'])?"checked":''; ?> /> | <br />
      
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>
   </body>
</html>